import React from "react";
import {connect} from "dva";
import EditableTable from "@/pages/flowmonitor/components/variablelist/RunningVariable/EditableTable";

const emptyParams = {
  processInstanceIds: [],
  name: '',
  taskId: '',
}
 @connect(({
            FlowMonitor, loading, appListMenu, Public,
}) => ({
  focusApp: appListMenu.focusApp,
  loading,
  menuDataList: Public.menuDataList,
  runningVariableList: FlowMonitor.runningVariableList,
  resourceList: Public.resourceList,
}))
export default class RunningVariable extends React.Component {
  state = {
    page: {
      pageNum: 1,
      pageSize: 10,
    },
  }

  searchParams = emptyParams;

   // eslint-disable-next-line no-useless-constructor
  constructor(props) {
    super(props);

  }

  componentDidMount() {
    this.initData();
  }

  componentDidUpdate(preProps) {
    const { focusApp } = this.props;
    const { focusApp: preFocusApp } = preProps;
    if (preFocusApp.id && `${preFocusApp.id || 0}` !== `${focusApp.id || 0}`) {
      // 应用切换更新页面数据
      this.resetPageData();
    }
  }

  resetPageData = () => {
    const { dispatch } = this.props;
    this.searchParams = emptyParams;
    if (this.tableForm) {
      this.tableForm.handleReset();
    }
    dispatch({ type: "FlowMonitor/set/runningVariableList", payload: { datas: [], totalCount: 0 } });
    this.setState({
        page: { pageNum: 1, pageSize: 10 },
      }, () => {
        this.initData();
      }
    );
  }

  initData = () => {
    const { page } = this.state;
    const { dispatch } = this.props;
    const temp = {...this.searchParams};
    if (this.searchParams.processInstanceIds && typeof (this.searchParams.processInstanceIds) === "string"){
      temp.processInstanceIds = this.searchParams.processInstanceIds.split(/,|，/);
    }
    if (!this.searchParams.processInstanceIds){
      this.searchParams.processInstanceIds = [];
      temp.processInstanceIds = [];
    }
    const params = { ...page, ...temp};
    dispatch({ type: 'FlowMonitor/runningVariableList', payload: params })
  }

  handleSearch = (values, reset) => {
    const { page } = this.state;
    this.searchParams = reset ? emptyParams : values;
    this.setState({ page: { ...page, pageNum: 1 } }, () => {
      this.initData();
    });
  }

  // 页码页数改变
  handlePageChange = (pageNum, pageSize) => {
    this.setState({ page: { pageNum, pageSize } }, () => {
      this.initData();
    });
  }

  render() {
    const { runningVariableList, dispatch} = this.props;
    return (
      // eslint-disable-next-line react/destructuring-assignment
/*        <SearchTable
          ref={r => {
          if (r) {
            this.tableForm = r;
          }
        }}
          formConfig={this.getFormConfig()}
          handleSearch={this.handleSearch}
          tableProps={this.getAntTableProps()}
        /> */

      <EditableTable
        runningVariableList={runningVariableList}
        /* eslint-disable-next-line react/destructuring-assignment */
        page={this.state.page}
        handlePageChange={this.handlePageChange}
        handleSearch={this.handleSearch}
        dispatch={dispatch}
      />
    );
  }
}

